package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class CookieController {
    //设置Cookie
    @GetMapping("/cookie1")
    public Result cookie1(HttpServletResponse response){
        //设置Cookie 响应Cookie给浏览器
        response.addCookie(new Cookie("login_username","wolfcode"));
        return Result.success();
    }

    //获取Cookie
    @GetMapping("/cookie2")
    public Result getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("login_username")){
                //输出username
                System.out.println("username="+cookie.getValue());
            }

        }
        return Result.success();
    }

}
